package com.indexingsystem.system.controller;


import com.indexingsystem.boss.Vo.*;
import com.indexingsystem.boss.common.PagedResult;
import com.indexingsystem.boss.common.ProgramLogsCommon;
import com.indexingsystem.boss.entity.IdxProducts;
import com.indexingsystem.boss.entity.IdxTaskCheck;
import com.indexingsystem.boss.entity.IdxTaskWithBLOBs;
import com.indexingsystem.boss.entity.ProgramLogs;
import com.indexingsystem.boss.logback.LogCvt;
import com.indexingsystem.boss.utils.DateUtil;
import com.indexingsystem.system.service.IProgramLogsService;
import com.indexingsystem.system.service.IdxTaskCheckService;
import com.indexingsystem.system.service.IdxTaskProductService;
import com.indexingsystem.system.service.IdxTaskService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/IndexingSystem/Service")
public class IdxTaskController {

    private ProgramLogs logs;
    @Resource
    private IProgramLogsService logsService;
    @Resource
    private IdxTaskService idxTaskService;
    @Resource
    private IdxTaskCheckService idxTaskCheckService;
    @Resource
    private IdxTaskProductService idxTaskProductService;


    /**
     * 6.8 审核人审核任务信息列表
     * @param reqVo
     * @return
     */
    @RequestMapping(value = "/task/getCheckTasksList", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getCheckTasksList(@RequestBody TaskProductListReqVo reqVo) {
        Map<String, Object> result = new HashMap<>();
        String message = "操作失败";
        String result_code = "9999";
        if (reqVo != null) {

        }
        if (null != reqVo && StringUtils.isNotBlank(reqVo.getTaskCheckBy())) {
            System.out.println(reqVo.toString());
            try {
                PagedResult<TaskProductListResVo> pagedResult = idxTaskService.getCheckTasksList(reqVo);
                List<TaskProductListResVo> data = pagedResult.getDataList();
                result.put("length", pagedResult.getTotal());
                result.put("data", data);
                message = "操作成功！";
                result_code = "0000";
            } catch (Exception ex) {
                LogCvt.error("审核人审核任务信息列表查询失败，原因：" + ex.getMessage());
                logs = ProgramLogsCommon.init();
                logs.setMessage("/Service/task/getCheckTasksList");
                logs.setLevel("1");
                logs.setLogger("ToDataBase");
                logs.setLogTime(DateUtil.parseFormatToDate(DateUtil.getCurTotalDate()));
                logs.setException(ex.toString());
                logsService.insertSelective(logs);

                message = "操作失败：" + ex.getMessage();

            }
        } else {
            message = "参数不全或错误！";
            result_code = "6666";
        }
        result.put("message", message);
        result.put("result_code", result_code);
        return result;
    }


    /**
     * 6.10 任务审核的历史列表
     * @param reqVo
     * @return
     */
    @RequestMapping(value = "/task/getAuditTaskList", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getAuditTaskList(@RequestBody GetAuditTaskListReqVo reqVo) {
        Map<String, Object> result = new HashMap<>();
        String message = "操作失败";
        String result_code = "9999";
        if (null != reqVo ) {
            System.out.println(reqVo.toString());
            try {
                PagedResult<IdxTaskCheck> pagedResult = idxTaskCheckService.getAuditTaskList(reqVo);
                List<IdxTaskCheck> data = pagedResult.getDataList();
                result.put("length", pagedResult.getTotal());
                result.put("data", data);
                message = "操作成功！";
                result_code = "0000";
            } catch (Exception ex) {
                LogCvt.error("任务审核的历史列表查询失败，原因：" + ex.getMessage());
                logs = ProgramLogsCommon.init();
                logs.setMessage("/Service/task/getAuditTaskList");
                logs.setLevel("1");
                logs.setLogger("ToDataBase");
                logs.setLogTime(DateUtil.parseFormatToDate(DateUtil.getCurTotalDate()));
                logs.setException(ex.toString());
                logsService.insertSelective(logs);

                message = "操作失败：" + ex.getMessage();

            }
        } else {
            message = "参数不全或错误！";
            result_code = "6666";
        }
        result.put("message", message);
        result.put("result_code", result_code);
        return result;
    }

    /**
     * 6.19 获取任务分配人未完成任务列表
     *
     * @param reqVo
     * @return
     */
    @RequestMapping(value = "/task/getUnFinishedTaskForAssignList", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getUnFinishedTaskForAssignList(@RequestBody TaskListReqVo reqVo) {
        Map<String, Object> result = new HashMap<>();
        String message = "操作失败";
        String result_code = "9999";
        if (null != reqVo && StringUtils.isNotBlank(reqVo.getAssignBy())) {
            System.out.println(reqVo.toString());
            try {
                reqVo.setCreateBy("");
                PagedResult<IdxTaskWithBLOBs> pagedResult = idxTaskService.getUnFinishedTaskList(reqVo);
                List<IdxTaskWithBLOBs> data = pagedResult.getDataList();
                result.put("length", pagedResult.getTotal());
                result.put("data", data);
                message = "操作成功！";
                result_code = "0000";
            } catch (Exception ex) {
                LogCvt.error("获取任务分配人未完成任务列表失败，原因：" + ex.getMessage());
                logs = ProgramLogsCommon.init();
                logs.setMessage("/Service/task/getUnFinishedTaskForAssignList");
                logs.setLevel("1");
                logs.setLogger("ToDataBase");
                logs.setLogTime(DateUtil.parseFormatToDate(DateUtil.getCurTotalDate()));
                logs.setException(ex.toString());
                logsService.insertSelective(logs);

                message = "操作失败：" + ex.getMessage();

            }
        } else {
            message = "参数不全或错误！";
            result_code = "6666";
        }
        result.put("message", message);
        result.put("result_code", result_code);
        return result;
    }
    /**
     * 6.20 获取任务任务创建人未完成任务列表
     *
     * @param reqVo
     * @return
     */
    @RequestMapping(value = "/task/getUnFinishedTaskForCreateByList", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getUnFinishedTaskForCreateByList(@RequestBody TaskListReqVo reqVo) {
        Map<String, Object> result = new HashMap<>();
        String message = "操作失败";
        String result_code = "9999";
        if (null != reqVo && StringUtils.isNotBlank(reqVo.getCreateBy())) {
            System.out.println(reqVo.toString());
            try {
                reqVo.setAssignBy("");
                PagedResult<IdxTaskWithBLOBs> pagedResult = idxTaskService.getUnFinishedTaskList(reqVo);
                List<IdxTaskWithBLOBs> data = pagedResult.getDataList();
                result.put("length", pagedResult.getTotal());
                result.put("data", data);
                message = "操作成功！";
                result_code = "0000";
            } catch (Exception ex) {
                LogCvt.error("获取任务创建人未完成任务列表失败，原因：" + ex.getMessage());
                logs = ProgramLogsCommon.init();
                logs.setMessage("/Service/task/getUnFinishedTaskForCreateByList");
                logs.setLevel("1");
                logs.setLogger("ToDataBase");
                logs.setLogTime(DateUtil.parseFormatToDate(DateUtil.getCurTotalDate()));
                logs.setException(ex.toString());
                logsService.insertSelective(logs);

                message = "操作失败：" + ex.getMessage();

            }
        } else {
            message = "参数不全或错误！";
            result_code = "6666";
        }
        result.put("message", message);
        result.put("result_code", result_code);
        return result;
    }

    /**
     * 6.21. 任务结案
     *
     * @param reqVo
     * @return
     */
    @RequestMapping(value = "/task/setTaskComplete", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> setTaskComplete(@RequestBody CompleteTaskReqVo reqVo) {
        Map<String, Object> result = new HashMap<>();
        String message = "操作失败";
        String result_code = "9999";
        Integer status = 0;
        if (null != reqVo && reqVo.isNotBlank()) {
            try {
                if (reqVo.getCompletestatus() != 0 && reqVo.getCompletestatus() != 1){
                    message = "状态参数不合法！";
                } else if (idxTaskService.selectByPrimaryKey(reqVo.getId()) == null) {
                    message = "该任务不存在！";
                } else if (idxTaskService.getParentIdByTaskId(reqVo.getId()) != 0) {
                    message = "该任务不是顶级任务！";
                } else {
                    if (reqVo.getCompletestatus() == 0) {
                        status = 1;
                    }
                    List<String> ids = idxTaskService.getArrchildidsByTaskId(reqVo.getId());
                    if (ids != null && ids.size() > 0) {
                        idxTaskProductService.updateRefIndexingByTaskIds(ids, status);
                        idxTaskService.updateCompleteStatusByIdBatch(reqVo.getCompletestatus(), ids);
                        message = "操作成功！";
                        result_code = "0000";
                    } else {
                        message = "该任务没有子任务！";
                    }
                }
            } catch (Exception ex) {
                LogCvt.error("任务结案状态设置失败，原因：" + ex.getMessage());
                logs = ProgramLogsCommon.init();
                logs.setMessage("/Service/task/setTaskComplete");
                logs.setLevel("1");
                logs.setLogger("ToDataBase");
                logs.setLogTime(DateUtil.parseFormatToDate(DateUtil.getCurTotalDate()));
                logs.setException(ex.toString());
                logsService.insertSelective(logs);

                message = "操作失败：" + ex.getMessage();
            }
        } else {
            message = "参数不全！";
            result_code = "6666";
        }

        result.put("message", message);
        result.put("result_code", result_code);
        return result;
    }

    /**
     * 6.22 主任务分配产品时选择的产品列表
     *
     * @param reqVo
     * @return
     */
    @RequestMapping(value = "/task/MainTaskProductlist", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> MainTaskProductlist(@RequestBody TaskProductAddReqVo reqVo) {
        Map<String, Object> result = new HashMap<>();
        LogCvt.info(reqVo.toString());
        String message = "操作失败";
        String result_code = "9999";
        if (null != reqVo && reqVo.getTaskId() != null ) {
            try {
                Integer taskCount = idxTaskService.selectCountByTaskIdAndStatus(reqVo);
                if (reqVo.getCompletestatus() != null && reqVo.getCompletestatus() != 0 && reqVo.getCompletestatus() != 1){
                    message = "状态参数不合法！";
                } else if (taskCount == null || taskCount < 1) {
                    message = "该状态下的任务不存在！";
                } else {
                    PagedResult<IdxProducts> pagedResult = idxTaskService.getUnassignProducts(reqVo);
                    result.put("data", pagedResult.getDataList());
                    result.put("length", pagedResult.getTotal());
                    message = "操作成功！";
                    result_code = "0000";
                }
            } catch (Exception ex) {
                LogCvt.error("主任务分配产品时选择的产品列表查询失败，原因：" + ex.getMessage());
                logs = ProgramLogsCommon.init();
                logs.setMessage("/Service/task/MainTaskProductlist");
                logs.setLevel("1");
                logs.setLogger("ToDataBase");
                logs.setLogTime(DateUtil.parseFormatToDate(DateUtil.getCurTotalDate()));
                logs.setException(ex.toString());
                logsService.insertSelective(logs);

                message = "操作失败：" + ex.getMessage();
            }
        } else {
            message = "参数不全！";
            result_code = "6666";
        }

        result.put("message", message);
        result.put("result_code", result_code);
        return result;
    }

    /**
     * 6.23	新增子任务时选择还未分配任务的产品列表
     * @param reqVo
     * @return
     */
    @RequestMapping(value = "/task/SubTaskUnAssignedProduct", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> SubTaskUnAssignedProduct(@RequestBody TaskProductAddReqVo reqVo) {
        Map<String, Object> result = new HashMap<>();
        String message = "操作失败";
        String result_code = "9999";
        LogCvt.info(reqVo.toString());
        if (null != reqVo && reqVo.getTaskId() != null) {
            try {
                if (idxTaskService.selectByPrimaryKey(reqVo.getTaskId()) == null) {
                    message = "该任务不存在！";
                } else if (idxTaskService.getParentIdByTaskId(reqVo.getTaskId()) == 0) {
                    message = "该任务是顶级任务，获取失败";
                } else {
                    PagedResult<IdxProductsResVo> pagedResult = idxTaskProductService.SubTaskUnAssignedProduct(reqVo);
                    if (pagedResult != null) {
                        List<IdxProductsResVo> data = pagedResult.getDataList();
                        result.put("length", pagedResult.getTotal());
                        result.put("data", data);
                        message = "操作成功！";
                        result_code = "0000";
                    } else {
                        result.put("length", 0);
                        result.put("data", null);
                        message = "无数据！";
                        result_code = "0000";
                    }
                }

            } catch (Exception ex) {
                LogCvt.error("新增子任务时选择还未分配任务的产品列表查询失败，原因：" + ex.getMessage());
                logs = ProgramLogsCommon.init();
                logs.setMessage("/Service/task/SubTaskUnAssignedProduct");
                logs.setLevel("1");
                logs.setLogger("ToDataBase");
                logs.setLogTime(DateUtil.parseFormatToDate(DateUtil.getCurTotalDate()));
                logs.setException(ex.toString());
                logsService.insertSelective(logs);

                message = "操作失败：" + ex.getMessage();
            }
        } else {
            message = "参数不全！";
            result_code = "6666";
        }

        result.put("message", message);
        result.put("result_code", result_code);
        return result;
    }
}
